package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Role;
import com.qk.management.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/roles")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @GetMapping
    public Result listRoles(@RequestParam(required = false) String name,
                            @RequestParam(required = false) String label,
                            @RequestParam(defaultValue = "1") Integer page,
                            @RequestParam(defaultValue = "10") Integer pageSize) {
        log.info("查询角色列表数据", name, label, page, pageSize);
        PageResult<Role> pageResult = roleService.listRoles(name, label, page, pageSize);
        return Result.success(pageResult);
    }

    @DeleteMapping("/{id}")
    public Result deleteRole(@PathVariable Integer id) {
        log.info("删除角色数据，ID: {}", id);
        roleService.deleteById(id);
        return Result.success();
    }

    @PostMapping
    public Result addRole(@RequestBody Role role) {
        log.info("添加角色数据: {}", role);
        roleService.addRole(role);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getRoleById(@PathVariable Integer id) {
        log.info("根据ID查询角色数据，ID: {}", id);
        Role role = roleService.getRoleById(id);
        return Result.success(role);
    }

    @PutMapping
    public Result updateRole(@RequestBody Role role) {
        log.info("修改角色数据: {}", role);
        roleService.updateRole(role);
        return Result.success();
    }

    // 新增查询所有角色的接口
    @GetMapping("/list")
    public Result listAllRoles() {
        log.info("查询所有角色数据");
        List<Role> roles = roleService.listAllRoles();
        return Result.success(roles);
    }

}
